﻿using System;
using fastCSharp;

namespace diantou.web.contest
{
    /// <summary>
    /// 排行榜
    /// </summary>
    abstract class rank<viewType> : problemView<viewType>
        where viewType : rank<viewType>
    {
        /// <summary>
        /// 排行榜
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected onlineJudge.judgeRank<diantou.dataAccess.contestJudge>[] ranks;
        /// <summary>
        /// 当前用户排行
        /// </summary>
        protected onlineJudge.judgeRank<diantou.dataAccess.contestJudge> topJudge;
        /// <summary>
        /// 当前用户排行
        /// </summary>
        protected onlineJudge.judgeRank<diantou.dataAccess.contestJudge> bottomJudge;
        /// <summary>
        /// 设置排行榜
        /// </summary>
        /// <param name="judges"></param>
        protected void load(diantou.dataAccess.contestJudge[] judges)
        {
            array.page arrayPage = new array.page(pageInfo.Count, pageInfo.Size, pageInfo.Page);
            int rank = arrayPage.SkipCount, userId = currentUserId, index = 0, isUser = 0;
            ranks = new onlineJudge.judgeRank<diantou.dataAccess.contestJudge>[arrayPage.CurrentPageSize];
            foreach (diantou.dataAccess.contestJudge judge in judges)
            {
                if (judge.UserId == userId)
                {
                    ranks[index++].Set(judge, ++rank, true);
                    isUser = 1;
                }
                else ranks[index++].Set(judge, ++rank, false);
            }
            if (userId != 0 && isUser == 0 && pageInfo.Count > pageInfo.Size)
            {
                diantou.dataAccess.contestJudge judge = getRank(out index);
                if (judge != null)
                {
                    if (index > rank) bottomJudge.Set(judge, index + 1, true);
                    else topJudge.Set(judge, index + 1, true);
                }
            }
        }
        /// <summary>
        /// 获取当前用户排行提交
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        protected abstract diantou.dataAccess.contestJudge getRank(out int index);
        /// <summary>
        /// 清除数据
        /// </summary>
        protected override void clear()
        {
            base.clear();
            topJudge.Judge = bottomJudge.Judge = null;
        }
    }
}
